package com.visikard.business;

import java.io.ByteArrayOutputStream;

import android.util.Log;

import com.visikard.application.Constant;
import com.visikard.item.ItemCouponsBuilderCreate;
import com.visikard.net.VKregisterNetworkException;
import com.visikard.request.CouponRequest;
import com.visikard.response.BaseResponse;
import com.visikard.response.CreateCouponResponse;
import com.visikard.response.DeleteCouponBuilderResponse;
import com.visikard.response.GetCouponDetailResponse;
import com.visikard.response.GetCouponResponse;
import com.visikard.response.GetRTUICouponResponse;
import com.visikard.response.RequestRTUICouponResponse;
import com.visikard.response.SearchAddressResponse;
import com.visikard.response.UpdateCouponResponse;
import com.visikard.response.UploadCBImageResponse;

public class CouponBUS {

	public GetCouponResponse getAllCoupons(long uid) {
		CouponRequest ar = new CouponRequest();
		GetCouponResponse result = new GetCouponResponse();
		try {
			String response = ar.getAllCoupons(uid);
			result.extractDataFromServer(response);
		}
		catch (VKregisterNetworkException e) {
			Log.e(Constant.LOG_TAG, "getAllCoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.NETWORK_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		catch (Exception e) {
			Log.e(Constant.LOG_TAG, "getAllCoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		return result;
	}

	public UploadCBImageResponse uploadCBImage(long userId, ByteArrayOutputStream baosphoto) {
		CouponRequest ar = new CouponRequest();
		UploadCBImageResponse result = new UploadCBImageResponse();
		try {
			String response = ar.getAllCoupons(userId);
			result.extractDataFromServer(response);
		}
		catch (VKregisterNetworkException e) {
			Log.e(Constant.LOG_TAG, "getAllCoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.NETWORK_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		catch (Exception e) {
			Log.e(Constant.LOG_TAG, "getAllCoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		return result;
	}

	public CreateCouponResponse createCoupon(long uid, String userId, ItemCouponsBuilderCreate item) {
		CouponRequest ar = new CouponRequest();
		CreateCouponResponse result = new CreateCouponResponse();
		try {
			String response = ar.getAllCoupons(uid);
			result.extractDataFromServer(response);
		}
		catch (VKregisterNetworkException e) {
			Log.e(Constant.LOG_TAG, "getAllCoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.NETWORK_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		catch (Exception e) {
			Log.e(Constant.LOG_TAG, "getAllCoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		return result;
	}

	public UpdateCouponResponse updateCoupon(long userId, ItemCouponsBuilderCreate item) {
		CouponRequest ar = new CouponRequest();
		UpdateCouponResponse result = new UpdateCouponResponse();
		try {
			String response = ar.getAllCoupons(userId);
			result.extractDataFromServer(response);
		}
		catch (VKregisterNetworkException e) {
			Log.e(Constant.LOG_TAG, "getAllCoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.NETWORK_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		catch (Exception e) {
			Log.e(Constant.LOG_TAG, "getAllCoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		return result;
	}

	public GetCouponDetailResponse getCouponDetail(long uid, int couponID) {
		CouponRequest ar = new CouponRequest();
		GetCouponDetailResponse result = new GetCouponDetailResponse();
		try {
			String response = ar.getAllCoupons(uid);
			result.extractDataFromServer(response);
		}
		catch (VKregisterNetworkException e) {
			Log.e(Constant.LOG_TAG, "getAllCoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.NETWORK_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		catch (Exception e) {
			Log.e(Constant.LOG_TAG, "getAllCoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		return result;
	}

	public DeleteCouponBuilderResponse deleteCouponsBuilder(long uid, Long fkUser, long id) {
		CouponRequest ar = new CouponRequest();
		DeleteCouponBuilderResponse result = new DeleteCouponBuilderResponse();
		try {
			String response = ar.getAllCoupons(uid);
			result.extractDataFromServer(response);
		}
		catch (VKregisterNetworkException e) {
			Log.e(Constant.LOG_TAG, "getAllCoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.NETWORK_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		catch (Exception e) {
			Log.e(Constant.LOG_TAG, "getAllCoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		return result;
	}

	public SearchAddressResponse searchAddress(long uid, String searchString) {
		CouponRequest ar = new CouponRequest();
		SearchAddressResponse result = new SearchAddressResponse();
		try {
			String response = ar.getAllCoupons(uid);
			result.extractDataFromServer(response);
		}
		catch (VKregisterNetworkException e) {
			Log.e(Constant.LOG_TAG, "getAllCoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.NETWORK_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		catch (Exception e) {
			Log.e(Constant.LOG_TAG, "getAllCoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		return result;
	}

	public GetRTUICouponResponse getRTUICoupons(String merchantIDFromRTUI) {
		CouponRequest ar = new CouponRequest();
		GetRTUICouponResponse result = new GetRTUICouponResponse();
		try {
			String response = ar.getRTUICoupons(merchantIDFromRTUI);
			result.extractDataFromServer(response);
		}
		catch (VKregisterNetworkException e) {
			Log.e(Constant.LOG_TAG, "getRTUICoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.NETWORK_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		catch (Exception e) {
			Log.e(Constant.LOG_TAG, "getRTUICoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		return result;
	}

	public RequestRTUICouponResponse requestRTUICoupon(long merchantId, String title, String contractNo, String message) {
		CouponRequest ar = new CouponRequest();
		RequestRTUICouponResponse result = new RequestRTUICouponResponse();
		try {
			String response = ar.requestRTUICoupon(merchantId, title, contractNo, message);
			result.extractDataFromServer(response);
		}
		catch (VKregisterNetworkException e) {
			Log.e(Constant.LOG_TAG, "requestRTUICoupon: " + e.getMessage());
			result.setErrorCode(BaseResponse.NETWORK_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		catch (Exception e) {
			Log.e(Constant.LOG_TAG, "requestRTUICoupon: " + e.getMessage());
			result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		return result;
	}

}
